<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;
use App\memories;
use DB;

class MemoriesController extends Controller
{
      public function index()
      {
        $memories = DB::table('memories')->orderBy('family')->where('deleted_at','=',NULL)->get();
        $memories_f = DB::table('memories')->orderBy('family')->where('deleted_at','<>',NULL)->get();
        return view('backend.memory.index',compact('memories','memories_f'));
      }

      public function store(Request $request)
      {  $dt=Carbon::now();
        $this->validate($request,[
          'name'=>'required',
          'family'=>'required',
          'content'=>'required',
        ]);

        $memories = memories::create([
          'name'=> $request->name,
          'content'=> $request->content,
          'family'=> $request->family,
          'type'=> $request->type,
          'recall_time'=> $dt
        ]);
        session()->flash('success','记忆项目创建成功！');
        return redirect()->back();
      }

      public function remeber($id)
      {
        $memories = memories::findOrFail($id);
        $dt=Carbon::now();
        switch ($memories->recall_style) {
          case '30Min':
              $memories->update([
              'recall_style' =>'1Hr',
              'recall_time'=> $dt->addMinutes(30)
            ]);
            return redirect()->back();
            break;
            case '1Hr':
                $memories->update([
                'recall_style' =>'8Hr',
                'recall_time'=> $dt->addHour()
              ]);
              return redirect()->back();
              break;
              case '8Hr':
                  $memories->update([
                  'recall_style' =>'1Day',
                  'recall_time'=> $dt->addHours(8)
                ]);
                return redirect()->back();
                break;
                case '1Day':
                    $memories->update([
                    'recall_style' =>'2Days',
                    'recall_time'=> $dt->addDay()
                  ]);
                  return redirect()->back();
                  break;
                  case '2Days':
                      $memories->update([
                      'recall_style' =>'3Days',
                      'recall_time'=> $dt->addDays(2)
                    ]);
                    return redirect()->back();
                    break;
                    case '3Days':
                        $memories->update([
                        'recall_style' =>'7Days',
                        'recall_time'=> $dt->addDays(3)
                      ]);
                      return redirect()->back();
                      break;
                      case '7Days':
                          $memories->update([
                          'recall_style' =>'2weeks',
                          'recall_time'=> $dt->addWeek()
                        ]);
                        return redirect()->back();
                        break;
                        case '2weeks':
                            $memories->update([
                            'recall_style' =>'1Month',
                            'recall_time'=> $dt->addWeeks(2)
                          ]);
                          return redirect()->back();
                          break;
                          case '1Month':
                              $memories->update([
                              'recall_style' =>'6Months',
                              'recall_time'=> $dt->addMonth()
                            ]);
                            return redirect()->back();
                            break;
                            case '6Months':
                                $memories->update([
                                'recall_style' =>'Finish',
                                'recall_time'=> $dt->addMonths(6)
                              ]);
                              return redirect()->back();
                              break;
                              case 'Finish':
                              $memories->delete();
                              return redirect()->back();
                                break;
        }

      }

      public function destroy($id)
    {

      $memories = memories::findOrFail($id);
      $memories->forceDelete();
      session()->flash('success','记忆项目删除成功！');
      return redirect()->back();
    }
}
